package com.gongye.courseselectionsystem.schedule;

import com.gongye.courseselectionsystem.mapper.CourseMapper;
import com.gongye.courseselectionsystem.pojo.vo.CourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author baorongchuan
 */
@Component
public class RedisScheduleTask {

    @Resource
    private RedisTemplate<String, CourseVo> redisTemplate;
    @Resource
    private CourseMapper courseMapper;

    /**
     * 每天23点初始化redis库
     */
    @Scheduled(cron = "0 0 23 1-31 1-12 ?")
    @Autowired
    private void redisInit() {
        redisTemplate.delete("courseVo");
        List<CourseVo> courseVos = courseMapper.optionalCourseVo();
        redisTemplate.opsForList().rightPushAll("courseVo", courseVos);

    }
}
